


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html id="htmlId">
<head>
  <title>Coverage Report :: ViennaConverterProvider</title>
  <style type="text/css">
    @import "../../.css/coverage.css";
  </style>
</head>

<body>
<div class="header"></div>

<div class="content">
<div class="breadCrumbs">
    [ <a href="../../index.html">all classes</a> ]
    [ <a href="../index.html">at.schauer.gregor.vienna.convert.vienna</a> ]
</div>

<h1>Coverage Summary for Class: ViennaConverterProvider (at.schauer.gregor.vienna.convert.vienna)</h1>

<table class="coverageStats">
<tr>
  <th class="name">Class</th>
<th class="coverageStat 
">
  Class, %
</th>
<th class="coverageStat 
">
  Method, %
</th>
<th class="coverageStat 
">
  Line, %
</th>
</tr>
<tr>
  <td class="name">ViennaConverterProvider</td>
<td class="coverageStat">
  <span class="percent">
    100%
  </span>
  <span class="absValue">
    (1/ 1)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    100%
  </span>
  <span class="absValue">
    (6/ 6)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    93.1%
  </span>
  <span class="absValue">
    (27/ 29)
  </span>
</td>
</tr>

</table>

<br/>
<br/>


<div class="sourceCode"><i>1</i>&nbsp;/*
<i>2</i>&nbsp; * Copyright 2012 Gregor Schauer
<i>3</i>&nbsp; *
<i>4</i>&nbsp; * Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<i>5</i>&nbsp; * you may not use this file except in compliance with the License.
<i>6</i>&nbsp; * You may obtain a copy of the License at
<i>7</i>&nbsp; *
<i>8</i>&nbsp; *      http://www.apache.org/licenses/LICENSE-2.0
<i>9</i>&nbsp; *
<i>10</i>&nbsp; * Unless required by applicable law or agreed to in writing, software
<i>11</i>&nbsp; * distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
<i>12</i>&nbsp; * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<i>13</i>&nbsp; * See the License for the specific language governing permissions and
<i>14</i>&nbsp; * limitations under the License.
<i>15</i>&nbsp; */
<i>16</i>&nbsp;package at.schauer.gregor.vienna.convert.vienna;
<i>17</i>&nbsp;
<i>18</i>&nbsp;import at.schauer.gregor.vienna.convert.factory.ConverterFactory;
<i>19</i>&nbsp;import at.schauer.gregor.vienna.convert.factory.ConverterFactoryAware;
<i>20</i>&nbsp;import at.schauer.gregor.vienna.convert.provider.AbstractConverterProvider;
<i>21</i>&nbsp;import at.schauer.gregor.vienna.exception.ViennaException;
<i>22</i>&nbsp;import at.schauer.gregor.vienna.util.ViennaUtils;
<i>23</i>&nbsp;import com.google.common.collect.HashBasedTable;
<i>24</i>&nbsp;import com.google.common.collect.Iterables;
<i>25</i>&nbsp;import com.google.common.collect.Table;
<i>26</i>&nbsp;import org.apache.commons.lang3.reflect.ConstructorUtils;
<i>27</i>&nbsp;import org.reflections.Reflections;
<i>28</i>&nbsp;import org.reflections.scanners.SubTypesScanner;
<i>29</i>&nbsp;
<i>30</i>&nbsp;import java.lang.reflect.Modifier;
<i>31</i>&nbsp;import java.lang.reflect.Type;
<i>32</i>&nbsp;import java.util.Map;
<i>33</i>&nbsp;import java.util.Set;
<i>34</i>&nbsp;
<i>35</i>&nbsp;/**
<i>36</i>&nbsp; * @author Gregor Schauer
<i>37</i>&nbsp; */
<b class="fc"><i>38</i>&nbsp;public class ViennaConverterProvider&lt;C extends SimpleConverter&gt; extends AbstractConverterProvider&lt;C&gt; implements ConverterFactory&lt;C&gt; {</b>
<b class="fc"><i>39</i>&nbsp;	Table&lt;Class&lt;?&gt;, Class&lt;?&gt;, C&gt; converterTable = HashBasedTable.create();</b>
<i>40</i>&nbsp;
<i>41</i>&nbsp;	@SuppressWarnings(&quot;unchecked&quot;)
<b class="fc"><i>42</i>&nbsp;	public ViennaConverterProvider() {</b>
<b class="fc"><i>43</i>&nbsp;		Reflections reflections = new Reflections(ViennaConverterProvider.class.getPackage().getName(), new SubTypesScanner(true));</b>
<b class="fc"><i>44</i>&nbsp;		Set&lt;Class&lt;? extends SimpleConverter&gt;&gt; classes = reflections.getSubTypesOf(SimpleConverter.class);</b>
<i>45</i>&nbsp;		try {
<b class="fc"><i>46</i>&nbsp;			for (Class&lt;? extends SimpleConverter&gt; clazz : classes) {</b>
<b class="fc"><i>47</i>&nbsp;				if (!Modifier.isAbstract(clazz.getModifiers()) &amp;&amp; !clazz.isInterface()) {</b>
<b class="fc"><i>48</i>&nbsp;					C c = (C) ConstructorUtils.invokeExactConstructor(clazz);</b>
<i>49</i>&nbsp;
<b class="fc"><i>50</i>&nbsp;					Map&lt;Type, Class&gt; genericTypes = ViennaUtils.TypeUtils.getGenericTypes(c.getClass());</b>
<b class="fc"><i>51</i>&nbsp;					Class type = Iterables.getFirst(genericTypes.values(), null);</b>
<b class="fc"><i>52</i>&nbsp;					if (type != null) {</b>
<b class="fc"><i>53</i>&nbsp;						register(type, c);</b>
<i>54</i>&nbsp;					}
<b class="fc"><i>55</i>&nbsp;					if (c instanceof ConverterFactoryAware) {</b>
<b class="fc"><i>56</i>&nbsp;						((ConverterFactoryAware) c).setConverterFactory(this);</b>
<i>57</i>&nbsp;					}
<b class="fc"><i>58</i>&nbsp;				}</b>
<i>59</i>&nbsp;			}
<b class="nc"><i>60</i>&nbsp;		} catch (Exception e) {</b>
<b class="nc"><i>61</i>&nbsp;			throw new ViennaException(e);</b>
<b class="fc"><i>62</i>&nbsp;		}</b>
<b class="fc"><i>63</i>&nbsp;	}</b>
<i>64</i>&nbsp;
<i>65</i>&nbsp;	@Override
<i>66</i>&nbsp;	public C register(Class&lt;?&gt; type, C converter) {
<b class="fc"><i>67</i>&nbsp;		type = getType(type);</b>
<b class="fc"><i>68</i>&nbsp;		converterTable.put(type, String.class, converter);</b>
<b class="fc"><i>69</i>&nbsp;		converterTable.put(String.class, type, converter);</b>
<b class="fc"><i>70</i>&nbsp;		return converter;</b>
<i>71</i>&nbsp;	}
<i>72</i>&nbsp;
<i>73</i>&nbsp;	@Override
<i>74</i>&nbsp;	public &lt;S, T&gt; C getConverter(Class&lt;S&gt; sourceClass, Class&lt;T&gt; targetClass) {
<b class="fc"><i>75</i>&nbsp;		return converterTable.get(getType(sourceClass), getType(targetClass));</b>
<i>76</i>&nbsp;	}
<i>77</i>&nbsp;
<i>78</i>&nbsp;	@Override
<i>79</i>&nbsp;	@SuppressWarnings(&quot;unchecked&quot;)
<i>80</i>&nbsp;	public &lt;X, Y&gt; Y convert(X source, Class&lt;Y&gt; targetClass) {
<b class="fc"><i>81</i>&nbsp;		C converter = getConverter(getType(source.getClass()), getType(targetClass));</b>
<b class="fc"><i>82</i>&nbsp;		if (source instanceof String) {</b>
<b class="fc"><i>83</i>&nbsp;			return (Y) converter.sourceToTarget((String) source, targetClass);</b>
<i>84</i>&nbsp;		} else {
<b class="fc"><i>85</i>&nbsp;			return (Y) converter.targetToSource(source);</b>
<i>86</i>&nbsp;		}
<i>87</i>&nbsp;	}
<i>88</i>&nbsp;
<i>89</i>&nbsp;	protected Class&lt;?&gt; getType(Class&lt;?&gt; clazz) {
<b class="fc"><i>90</i>&nbsp;		return clazz.isArray() ? Object[].class : clazz;</b>
<i>91</i>&nbsp;	}
<i>92</i>&nbsp;}
</div>
</div>

<div class="footer">
    
    <div style="float:right;">generated on 2012-07-17 20:23</div>
</div>
</body>
</html>
